from machine import Pin
from utime import sleep
from machine import SoftI2C
from ssd1306 import SSD1306_I2C
import onewire
import ds18x20

def ds18b20_temp(ds18b20, oled):
    # 扫描设备
    roms = ds18b20.scan()
    while len(roms):
        # 温度转换
        ds18b20.convert_temp()
        temp = ds18b20.read_temp(roms[0])
        print("DS18B20 TEMP: %.2f℃" % temp)
        
        # oled显示
        oled.fill(0)
        oled.text("DS18B20 TEMP", 18, 5, 1)
        oled.text("TEMP: %.2f C" % temp, 10, 20, 1)
        oled.show()
        sleep(1)
    print("未扫描到任何onewire设备~")

if __name__ == "__main__":
    # 创建DS18B20对象
    ds18b20 = ds18x20.DS18X20(onewire.OneWire(Pin(25)))
    # 创建LED
    oled = SSD1306_I2C(128, 64, SoftI2C(scl=Pin(18), sda=Pin(23)))
    
    ds18b20_temp(ds18b20, oled)
        
        
        
